import axios from "axios";


const service = axios.create({
  baseURL: './api',
  timeout: 80000,
});

// 1.添加请求拦截器
service.interceptors.request.use(
  (config) => {
    config.headers.common = {
      Accept: "application/json",
      "Content-Type": "application/json",
    };
    // 在发送请求之前做些什么（在请求头中加token）
    // let token = localStorage.getItem('token');
    // let headersObj = config.headers || {}
    // if (!headersObj.hasOwnProperty('Authorization') && token) {
    //   config.headers.Authorization = token;
    // }
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

//3.添加响应拦截器
service.interceptors.response.use(
  function (response) {
    return response;
  },
  function (error) {
    // Message('网络错误请重试');
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

export default service;

export const get = (url: string, config: object = {}) => {
  return new Promise((resolve, reject) => {
    service
      .get(url, {
        ...config,
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};


export const post = (url: string, config: object = {}) => {
  const { data, ...rest } = config as any;
  return new Promise((resolve, reject) => {
    service
      .post(url, data, {
        ...rest,
        // paramsSerializer(params) {
        //   return qs.stringify(params, {
        //     arrayFormat: "repeat",
        //   });
        // },
      })
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
